package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.IDishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-25
 */
@Slf4j
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
@RestController
public class DishController {
    @Autowired
    private IDishService dishService;

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result page(DishPageQueryDTO dto) {
        PageResult list = dishService.pageQuery(dto);
        return Result.success(list);
    }
    @ApiOperation("新增菜品")
    @PostMapping
    private Result add(@RequestBody DishDTO dto){
      /* dishService.add(dto);
        return Result.success();*/
       log.info("新增菜品：{}", dto);
        dishService.add(dto);

        //清理缓存数据
        String key = "dish_" + dto.getCategoryId();
        cleanCache(key);
        return Result.success();
    }
    @ApiOperation("根据id查询菜品")
    @GetMapping("{id}")
    public Result<DishVO>getById(@PathVariable Integer id){
        DishVO vo = dishService.getdishById(id);
        return Result.success(vo);

    }

    @ApiOperation("更新菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dto){
        /*dishService.updatecaipin(dto);
        return Result.success();*/
        log.info("修改菜品：{}", dto);
        dishService.updatecaipin(dto);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");

        return Result.success();
    }

    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result delete(@RequestParam List ids) {
       /* dishService.deleteById(ids);
        return Result.success();*/
        log.info("菜品批量删除：{}", ids);
        dishService.deleteById(ids);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");

        return Result.success();
    }
    @ApiOperation("菜品起售、停售")
    @PostMapping("/status/{status}")
    public Result status(  @PathVariable("status") Integer status ,@RequestParam("id") Integer id ){
      /*  dishService.status(status,id);
        return Result.success();*/
        dishService.status(status, id);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");

        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("菜品列表")
    public Result <List<Dish>> getlistId(Integer categoryId){
         List<Dish>list= dishService.getlistId(categoryId);
        return Result.success(list);

    }
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }





}
